package com.cskaoyan.wx.controller;

import com.cskaoyan.vo.BaseRespVo;
import com.cskaoyan.wx.bean.WxOrderGoods;
import com.cskaoyan.wx.bo.SubmitBO;
import com.cskaoyan.wx.bo.WxOrderBO;
import com.cskaoyan.wx.bo.WxOrderCommentBO;
import com.cskaoyan.wx.service.WxOrderService;
import com.cskaoyan.wx.vo.WxOrderDetailVO;
import com.cskaoyan.wx.vo.WxOrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author： aurora
 * @data： 2021-05-12 11:01
 */

@RestController
@RequestMapping("wx/order")
public class WxOrderController {

    @Autowired
    private WxOrderService wxOrderService;

    //获取订单列表
    @GetMapping("list")
    public BaseRespVo orderList(WxOrderBO wxOrderBO){
        WxOrderVO wxOrderVO = wxOrderService.getOrder(wxOrderBO);
        return BaseRespVo.ok(wxOrderVO);
    }

    //获取具体订单信息
    @GetMapping("detail")
    public BaseRespVo orderDetail(Integer orderId){
        WxOrderDetailVO wxOrderDetailVO = wxOrderService.getOrderDetail(orderId);
        return BaseRespVo.ok(wxOrderDetailVO);
    }

    //评价订单前获取商品
    @GetMapping("goods")
    public BaseRespVo WxOrderGoods (Integer orderId, Integer goodsId){
        WxOrderGoods wxOrderGoods = wxOrderService.getWxOrderGoods(orderId, goodsId);
        return BaseRespVo.ok(wxOrderGoods);
    }

    //评价：前台无法查看评价内容？  order_goods表修改：comment = comment表中的id
    //comment表新增：数值，order表修改：comments:待评价订单商品数量
    @PostMapping("comment")
    public BaseRespVo orderComment(@RequestBody WxOrderCommentBO wxOrderCommentBO){
        //7天后商品超期不能评价，comment = -1,order表中的confirm_time为起始时间，同时修改order表中的comments字段为0
        // 通过时间触发器已实现（状态码是否标记为已评价？）
        wxOrderService.updateComment(wxOrderCommentBO);
        return BaseRespVo.ok();
    }

    //删除
    @PostMapping("delete")
    public BaseRespVo orderDelete(@RequestBody Map<String, Object> map){
        wxOrderService.orderDelete((Integer) map.get("orderId"));
        return BaseRespVo.ok();
    }

    //TODO：正在申请退款无法实现
    //退款,状态码：203已退款, 202申请退款
    @PostMapping("refund")
    public BaseRespVo orderRefund(@RequestBody Map<String, Object> map){
        wxOrderService.orderRefund((Integer) map.get("orderId"));
        return BaseRespVo.ok();
    }

    //系统取消（半小时后），通过时间触发器已实现
    //取消订单，状态码：102用户取消，103系统取消
    @PostMapping("cancel")
    public BaseRespVo orderCancle(@RequestBody Map<String, Object> map){
        wxOrderService.orderCancle((Integer) map.get("orderId"));
        return BaseRespVo.ok();
    }

    //系统收货(7天后)，暂无法实现👉ship_time无法获取,通过时间触发器已实现
    //确认收货，状态码：401用户收货，402系统收货
    @PostMapping("confirm")
    public BaseRespVo orderConfirm(@RequestBody Map<String, Object> map){
        wxOrderService.orderConfirm((Integer) map.get("orderId"));
        return BaseRespVo.ok();
    }

    //TODO:付款，状态码：201已付款(付款功能未实现)
    @PostMapping("prepay")
    public BaseRespVo prepay(@RequestBody Map<String, Object> map){
        /*wxOrderService.orderPrePay((Integer) map.get("orderId"));
        return BaseRespVo.fail("订单支付成功", 0);*/
        return BaseRespVo.fail("订单不能支付", 724);
    }

    //购物车提交订单
    @PostMapping("submit")
    public BaseRespVo submit(@RequestBody SubmitBO submitBO) {
        Integer orderId = wxOrderService.commit(submitBO);
        if (orderId == -1) {
            return BaseRespVo.fail("库存不够");
        }
        HashMap<String, Integer> result = new HashMap<>();
        result.put("orderId",orderId);
        return BaseRespVo.ok(result);
    }
}
